package cn.edu.imau.zy.system.controller;

import cn.edu.imau.zy.common.annotation.LoginUser;
import cn.edu.imau.zy.common.core.controller.BaseController;
import cn.edu.imau.zy.common.core.domain.R;
import cn.edu.imau.zy.common.core.entity.system.SysSignDetail;
import cn.edu.imau.zy.common.core.entity.system.SysSignJobDetailVo;
import cn.edu.imau.zy.common.core.entity.system.SysUser;
import cn.edu.imau.zy.system.service.ISysSignDetailService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 任务详情 提供者
 * 
 * @author wyz
 * @date 2020-07-07
 */
@RestController
@RequestMapping("signdetail")
public class SysSignDetailController extends BaseController
{
	
	@Autowired
	private ISysSignDetailService sysSignDetailService;
	
	/**
	 * 查询${tableComment}
	 */
	@GetMapping("get/{id}")
	public SysSignDetail get(@PathVariable("id") String id)
	{
		return sysSignDetailService.selectSysSignDetailById(id);
	}

	/**
	 * 查询我的签到任务列表
	 */
	@GetMapping("my")
	public R get(@LoginUser SysUser user)
	{
		return R.data(sysSignDetailService.selectMySignList(user));
	}

	/**
	 * 查询我的签到任务详情
	 */
	@GetMapping("myinfo/{id}")
	public R myinfo(@PathVariable("id") String id)
	{
		return R.data(sysSignDetailService.selectMySign(id));
	}

	/**
	 * 完成我的任务
	 */
	@PostMapping("finish")
	public R finish(@LoginUser SysUser user,@RequestBody SysSignDetail signDetail)
	{
		SysSignJobDetailVo sign = sysSignDetailService.selectMySign(signDetail.getId());

        if (!sign.getUserId().equals(getCurrentUserId())) {
			return R.error("非法操作");
		}

		return sysSignDetailService.finish(sign,signDetail)?R.ok(): R.error("您不在签到范围内");
	}
	
	/**
	 * 查询任务详情列表
	 */
	@PostMapping("list")
	public R list(@RequestBody Map<String,Object> sysSignDetail)
	{
		startPage();
        return result(sysSignDetailService.selectSysSignDetailList(sysSignDetail));
	}
	
	
	/**
	 * 新增保存任务详情
	 */
	@PostMapping("save")
	public R addSave(@RequestBody SysSignDetail sysSignDetail,@LoginUser SysUser user)
	{
        sysSignDetail.setCreateBy(user.getUserId());
		return toAjax(sysSignDetailService.insertSysSignDetail(sysSignDetail));
	}

	/**
	 * 修改保存任务详情
	 */
	@PostMapping("update")
	public R editSave(@RequestBody SysSignDetail sysSignDetail,@LoginUser SysUser user)
	{
		sysSignDetail.setUpdateBy(user.getUserId().toString());
		return toAjax(sysSignDetailService.updateSysSignDetail(sysSignDetail));
	}
	
	/**
	 * 删除${tableComment}
	 */
	@PostMapping("remove")
	public R remove(String ids)
	{		
		return toAjax(sysSignDetailService.deleteSysSignDetailByIds(ids));
	}
	
}
